const fs = require('fs');

const momentService = require('../service/moment.service')
const fileService = require('../service/file.service');
const { PICTURE_PATH } = require('../constants/file-path');

class MomentController {
    async create(ctx, next) {
      console.log('ddd0990')
        console.log(ctx.body)
        // 1.获取数据
        const userId = ctx.user.id;
        const { content, title } = ctx.request.body
        console.log('title,content', title, content)
        // 2.将数据插入到数据库中
        const result = await momentService.create(userId, content, title)
        ctx.body  = result
    }

    async detail(ctx, next) {
        let { momentId } = ctx.params
        let { userId } = ctx.query
        console.log('userId',userId,momentId)
        if(!userId) {
          userId = 0
        }
        const result = await momentService.getMomentById(userId, momentId)
        console.log(result,'result')
        ctx.body = result
    }

    async list(ctx, next) {
      try {
        console.log(ctx,'xtc')
        let { offset, size, userId } = ctx.query
        console.log('userId',userId)
        if(!userId) {
          userId = 0
        }
        const result = await momentService.getMomentList(userId, offset, size)
        ctx.body = result
      } catch (error) {
        console.log(error)
      }

    }

    async hotlist(ctx, next) {
      console.log('d')
      const { offset, size } = ctx.query
      let { userId } = ctx.query
      console.log('userId',userId)
      if(!userId) {
        userId = 0
      } 
      console.log('offset size', offset,size)
      const result = await momentService.getHotMomentList(userId, offset, size)
      ctx.body = result
    }


    async update(ctx, next) {
        const { momentId } = ctx.params
        const { content } = ctx.request.body
        const result = await momentService.update(momentId, content)
        ctx.body = result
    }

    async remove(ctx, next) {
        const { momentId } = ctx.params
        const result = await momentService.remove(momentId)
        ctx.body = result
    }

    
  async addLabels(ctx, next) {
    // 1.获取标签和动态id
    const { labels } = ctx;
    const { postsId } = ctx.params;

    // 2.添加所有的标签
    for (let label of labels) {
      // 2.1.判断标签是否已经和动态有关系
        const isExist = await momentService.hasLabel(postsId, label.id);
      if (!isExist) {
        await momentService.addLabel(postsId, label.id);
      }
    }

    ctx.body = "给动态添加标签成功~";
  }

  async fileInfo(ctx, next) {
    console.log('first')
    let { filename } = ctx.params;
    console.log('filename',filename)
    const fileInfo = await fileService.getFileByFilename(filename);
    // const { type } = ctx.query;
    // const types = ["small", "middle", "large"];
    // if (types.some(item => item === type)) {
    //   filename = filename + '-' + type;
    // }
    console.log('fileinfo',fileInfo)
    ctx.response.set('content-type', fileInfo.mimetype);
    ctx.body = fs.createReadStream(`${PICTURE_PATH}/${filename}`);
  }

  async momentList(ctx, next) {
    const { userId, offset, size } = ctx.query
    const result = await momentService.getUserMomentList(userId, offset, size)
    ctx.body = result
  }

  async userMomentList(ctx, next) {
    const userId = ctx.user.id
    const result = await momentService.getUserMomentListById(userId)
    ctx.body = result
  }
}

module.exports = new MomentController()
